package com.konsung.ksdc.manager.shop;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.OrderItemNetBean;
import com.konsung.ksdc.common.network.RequestCallback;
import com.konsung.ksdc.common.network.RequestUtils;
import com.konsung.ksdc.common.network.ResponseBean;
import com.konsung.ksdc.common.widget.WaitingDialog;
import com.konsung.ksdc.config.MyApplication;
import com.konsung.ksdc.config.NetConfig;
import com.konsung.ksdc.manager.appmain.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * 我的订单
 * Created by DJH on 2017/2/28 0028.
 */
public class MyOrderActivity extends BaseActivity {
    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tab_tv_title)
    TextView tvTitle;
    @Bind(R.id.ll_empty)
    LinearLayout llEmpty;
    @Bind(R.id.lv_order_list)
    ListView lvOrderList;
    @Bind(R.id.btn_go)
    Button btnGo;
    @Bind(R.id.iv_abnormal)
    ImageView ivAbnormal;
    @Bind(R.id.tv_abnormal_type)
    TextView tvAbnormalType;
    @Bind(R.id.tv_abnormal_text)
    TextView tvAbnormalText;

    public static final int MSG_UPDATE_ORDER = 10010;
    public static final int MSG_EMPTY_ORDER = 1;
    public static final int MSG_ERROR_ORDER = 2;
    public static final String ORDER_DATA = "order_data";
    private WaitingDialog waitingDialog;
    private ArrayList<OrderItemNetBean> orderBeanList = new ArrayList<>();
    private OrderTypeAdapter orderTypeAdapter;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case MSG_UPDATE_ORDER: //更新订单页面的UI
                    llEmpty.setVisibility(View.GONE);
                    waitingDialog.dismiss();
                    orderTypeAdapter = new OrderTypeAdapter(MyOrderActivity.this, orderBeanList);
                    lvOrderList.setAdapter(orderTypeAdapter);
                    orderTypeAdapter.notifyDataSetChanged();
                    break;
                case MSG_EMPTY_ORDER:
                    waitingDialog.dismiss();
                    llEmpty.setVisibility(View.VISIBLE);
                    tvAbnormalType.setText(R.string.tip_order_none);
                    tvAbnormalText.setVisibility(View.GONE);
                    btnGo.setVisibility(View.GONE);
                    break;
                case MSG_ERROR_ORDER:
                    waitingDialog.dismiss();
                    llEmpty.setVisibility(View.VISIBLE);
                    ivAbnormal.setImageResource(R.drawable.connect_server_fail);
                    tvAbnormalType.setText(R.string.get_data_fail);
                    tvAbnormalText.setVisibility(View.GONE);
                    btnGo.setText(R.string.reload);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected String activityDesc() {
        return getString(R.string.page_order_form);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order);
        ButterKnife.bind(this);
    }

    /**
     * 初始化订单列表数据
     */
    private void initData() {
        if (llEmpty != null) {
            //重新加载数据，不显示图片,只显示进度条
            llEmpty.setVisibility(View.GONE);
        }
        if (waitingDialog == null) {
            waitingDialog = new WaitingDialog(this, "");
            waitingDialog.setText(getString(R.string.tip_loader));
            waitingDialog.show();
        } else {
            waitingDialog.show();
        }

        String userId = NetConfig.currentUser.getUserId();
        if (!RequestUtils.checkNetwork(this)) {
            waitingDialog.dismiss();
            llEmpty.setVisibility(View.VISIBLE);
            ivAbnormal.setImageResource(R.drawable.no_wifi);
            tvAbnormalType.setText(R.string.wifi_not_well);
            tvAbnormalText.setVisibility(View.VISIBLE);
            tvAbnormalText.setText(R.string.check_your_network);
            btnGo.setText(R.string.refresh);
            return;
        }
        RequestUtils.findAllOrders(userId, new RequestCallback() {
            @Override
            public void onError(Call call, Exception e) {
                handler.sendEmptyMessage(MSG_ERROR_ORDER);
            }

            @Override
            public void onResponse(ResponseBean responseBean) {
                orderBeanList = (ArrayList<OrderItemNetBean>)
                        responseBean.getList(new TypeToken<List<OrderItemNetBean>>() {
                        }.getType());
                if (orderBeanList != null && orderBeanList.size() > 0) {

                    handler.sendEmptyMessage(MSG_UPDATE_ORDER);
                } else {
                    handler.sendEmptyMessage(MSG_EMPTY_ORDER);
                }
            }
        });
    }

    /**
     * 初始化订单列表界面
     */
    private void initView() {
        tvTitle.setText(R.string.title_my_order);
        rlBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        btnGo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
        lvOrderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                OrderItemNetBean orderTypeBean = orderBeanList.get(position);
                Intent intent = new Intent(MyApplication.getContext(),
                        OrderCommitOkActivity.class);
                intent.putExtra(ORDER_DATA, orderTypeBean);
                intent.putExtra(OrderCommitOkActivity.ORDER_TYPE, "2");
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        handler.removeMessages(MSG_UPDATE_ORDER);
    }
}
